using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class HeroManager : MonoBehaviour
{
	private GameObject avatar;
	public PagePropertie currentPage;
	
	private bool jump, canJump;
	private float speed_X, speed_Y;
	private float jumpTime;
	private float base_jump;
	
	public HeroManager ()
	{
	}
	
	public void Build()
	{
		avatar = new GameObject();
		avatar.AddComponent("Square");
		Square tmp = (Square) avatar.GetComponent("Square");
		
		tmp.transform.parent = this.transform;
		
		tmp.Build(new Vector3(0,0,0), 
					new Vector3(0,0.8f,0),
                    new Vector3(0.8f, 0.8f, 0),
                    new Vector3(0.8f, 0, 0),
					
                          new Vector2(0,0), 
					new Vector2(0,1),
                    new Vector2(1,1), 
					new Vector2(1,0), 
					      
					      "Stickman","Hero");
		
		avatar.renderer.material = (Material)Resources.Load("StickmanMat");
		jump = false;
		canJump = true;
		jumpTime = 0;
		
	}
	
	void Start()
	{
		float x = currentPage.left_limit + transform.position.x;
		float y = currentPage.floor_height - transform.position.y;
		
		transform.Translate(new Vector3(x,y,0f));
	}
	
	void Update()
	{
		float SpeedStep = Time.deltaTime;
		float decelerationFactor = 7f/10f;
		float maxSpeed = 0.5f;
		float maxJump = .8f;
		if (Input.GetKey(KeyCode.D) && !jump)
		{
			speed_X += SpeedStep * 0.8f;
			if(speed_X < -maxSpeed) speed_X = -maxSpeed;
		}
		
		if(Input.GetKey(KeyCode.Q) && !jump)
		{
			speed_X -= SpeedStep * 0.8f;
			if(speed_X > maxSpeed) speed_X = maxSpeed;
		}
		
		if(Input.GetKey(KeyCode.Space) && canJump && !jump)
		{
			speed_Y += SpeedStep*3f;
			if(speed_Y > maxJump) speed_Y = maxJump;
			jump = true;
			base_jump = transform.position.y;
		}
		else if(Input.GetKey(KeyCode.Space) && canJump)
		{
			speed_Y += SpeedStep*3f;
			if(speed_Y > maxJump) speed_Y = maxJump;
			//jump = true;
		}
		
		if(jump)
		{
			jumpTime += Mathf.PI * SpeedStep * 2.5f;
			if(jumpTime > Mathf.PI / 2f)
			{
				canJump = false;	
			}
			if(jumpTime > Mathf.PI)
			{
				jumpTime = Mathf.PI;
			}
		}
		
		float incrementX = speed_X;
		
		if(transform.position.x + incrementX < currentPage.left_limit)
		{
			incrementX = currentPage.left_limit - transform.position.x;
		}
		else if (transform.position.x + incrementX > currentPage.right_limit - 0.8f)
		{
			incrementX = currentPage.right_limit - 0.8f - transform.position.x;
		}
		
		//update position
		if(jump) transform.position = new Vector3 (transform.position.x + incrementX, base_jump + speed_Y * Mathf.Sin(jumpTime), transform.position.z);
		else transform.position = new Vector3 (transform.position.x + incrementX, transform.position.y, transform.position.z);
		
		if(speed_X < 0 && !jump)
		{
			speed_X += decelerationFactor * SpeedStep;
			if(speed_X > 0) speed_X = 0;
		}
		
		if(speed_X > 0 && !jump)
		{
			speed_X -= decelerationFactor * SpeedStep;;
			if(speed_X < 0) speed_X = 0;
		}
		
		if(jumpTime >= Mathf.PI)
		{
			jump = false;
			canJump = true;
			speed_Y = 0f;
			jumpTime = 0f;
		}
		
	}
}


